package web.usuarios;

import Persistencia.*;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import controladores.*;
import dominio.SesionDomain;
import java.util.HashMap;
import java.util.Map;

public class ControladorSimpleSesion extends SimpleFormController{

    ControladorSesion controlador = new ControladorSesion();
    ControladorPaciente controladorPac = new ControladorPaciente();
    SesionDomain obj;

    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
        obj = ((SesionDomain) command);
        controlador.setSesionD(obj);
        String res = controlador.guardarSesion();
        Map<String, Object> modelo = new HashMap();
        if(res.split(" ")[0].equalsIgnoreCase("Se")){
            modelo.put("mensajeOk", res);
            modelo.put("mensajeNo", "");
            modelo.put("direccion", "IngresarSesion.htm?id_fisio="+obj.getIdFisioterapeuta());
            return new ModelAndView("resultadoValidator","model",modelo);
        }else{
            modelo.put("mensajeOk", "");
            modelo.put("mensajeNo", res);
            modelo.put("direccion", "IngresarSesion.htm?id_fisio="+obj.getIdFisioterapeuta());
            return new ModelAndView("resultadoValidator","model",modelo);
        }      
    }

    protected Object formBackingObject(HttpServletRequest request) throws ServletException {
        SesionDomain log = new SesionDomain();
        System.out.println(request.getParameter("id_fisio")+"   idFISIO");
        log.setIdFisioterapeuta(request.getParameter("id_fisio"));
        return log;
    }

    public void setControladorSesion(ControladorSesion productManager) {
        this.controlador = productManager;
    }

    public ControladorSesion getControladorSesion() {
        return controlador;
    }


    @SuppressWarnings("unchecked")
    protected Map referenceData(HttpServletRequest request) throws Exception {
        Map referenceData = new HashMap();
        referenceData.put("listPaciente", controladorPac.encontrarPacientesSinAsginar(request.getParameter("id_fisio")));
        return referenceData;
    }
}
